package com.sz.app.oss.api.common.customer;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.dto.CustomerMessageRelDto;
import com.sz.biz.common.customer.service.CustomerMessageRelService;
import com.sz.biz.common.customer.service.CustomerMessageRelTypeService;
import com.sz.biz.common.customer.dto.CustomerMessageRelTypeItem;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: Johnboy <br>
 * Date: 2017-08-11 10:47:00
 */
@RestController
@RequestMapping("${api}/customer/message")
@Api(description = " ", tags = "037-007、客户信息管理-客户消息配置")
public class CusCustomerMessageTypeController extends BaseController{
    @Autowired
    private CustomerMessageRelTypeService customerMessageRelTypeService;

    @Autowired
    private CustomerMessageRelService customerMessageRelService;

    @ApiOperation(value = "获取客户消息选择列表", notes = "获取客户消息选择列表", httpMethod = "GET", position = 1)
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public ResultDto getMessageTypeTree(){
        CustomerMessageRelTypeItem root = customerMessageRelTypeService.getMessageRelTypeTree(false,PrincipalUtils.getLocalLanguage());
        ResultDto resultDto = new ResultDto();
        resultDto.setData(root);
        return  resultDto;
    }

    @ApiOperation(value = "获取客户已结选择的消息列表", notes = "获取客户已结选择的消息列表", httpMethod = "GET", position = 2)
    @RequestMapping(value = "/tree/{customerId}",method = RequestMethod.GET)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT, Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    public ResultDto getCustomerMessageTypeTree(@ApiParam(required = true) @PathVariable int customerId){
        CustomerMessageRelTypeItem root = customerMessageRelTypeService.getCustomerMessageRelTypeTree(customerId);
        ResultDto resultDto = new ResultDto();
        resultDto.setData(root);
        return resultDto;
    }

    @ApiOperation(value = "保存客户的选择数据", notes = "保存客户的选择数据", httpMethod = "POST", position = 3)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value ="/{customerId}",method = RequestMethod.POST)
    public ResultDto selectCustomerMessageType(@ApiParam(required = true)  @RequestBody List<CustomerMessageRelDto> customerMessageRelDto,@ApiParam(required = true) @PathVariable int customerId){
        customerMessageRelService.save(customerMessageRelDto, customerId);
        ResultDto resultDto = getOperateSuccessDto(MessagesCodes.MODUEL_CUSTOMER_MESSAGE_TYPE_SAVE_SUCCESS);
        return resultDto;
    }
}
